<?php

class Zend_View_Helper_JQueryTableDataGrid extends ZendX_JQuery_View_Helper_UiWidget
{	
	protected $_nameTable;
	protected $_actions;
	protected $_href;
	
	public function jqueryTableDataGrid(Zend_Paginator $paginator,Array $tableHeaders,Array $tableDatas, Array $actions, Array $href)
	{
		$this->_nameTable = trim($href['module'].'-'.$href['controller'].'-'.$tableName.'TableGrid');
		$this->_actions = $actions;
		$this->_href = $href;
		
		$this->_initJavaScript();
		$this->_initHtml($paginator,$tableHeaders,$tableDatas,$actions);
		
	}
	
	private function _getActions($id) {
		$baseAsset = Zend_Controller_Front::getInstance()->getBaseUrl().'/public/assets/';

		$allActions = array('view','update','delete','print');
		$allowedActions = array_intersect($allActions,$this->_actions);
		$finalOptions = null;

		foreach($allActions as $option) {
			if(in_array($option,$allowedActions)) {
				$url = $this->view->url(array('module' => $this->_href['module'], 'controller'=> $this->_href['controller'],'action'=> $option,'id'=> $id), null, true);
				
				$finalOptions .= '<a href="'.$url.'"><img src="'.$baseAsset.'/img/layout/icons/action_small_'.$option.'.png" alt="'.$option.'" border=0"/></a>';
			} else {
				$finalOptions .= '<img src="'.$baseAsset.'/img/layout/icons/action_small_'.$option.'_disabled.png" alt="'.$option.'" border=0"/>';
			}	
		}
		
		return $finalOptions;
	}
	
	private function _initJavaScript() {
		$highLightScript = "
      		$(document).ready(function(){
				$('.table-datagrid-highlight').hover(function(){
					$(this).children().addClass('table-datagrid-highlight-on');
				},function(){
					$(this).children().removeClass('table-datagrid-highlight-on');
				});
      		});
		";
		
		$tableSorterScript = "
      		$(document).ready(function(){
				$('#".$this->_nameTable."').tablesorter(
				);
      		});
		";
				
		$this->jquery->addOnLoad($highLightScript);		
		$this->jquery->addOnLoad($tableSorterScript);		
	}
	
	private function _initHtml(Zend_Paginator $paginator,Array $tableHeaders,Array $tableDatas,Array $actions) {
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		if(isset($paginator)) {
			$count = count($paginator);
			if(true === (boolean)$count) {
				
				$allowedActions = array_intersect($this->_allActions,$actions);
				$finalOptions = null;
				
				foreach($allActions as $option) {
					if(in_array($option,$allowedActions)) {
						$finalOptions .= '<a href="'.$option.'/id/'.(int)$dataId.'"><img src="'.$baseAsset.'/img/layout/icons/action_small_'.$option.'.png" alt="'.$option.'" border=0"/></a>';
					} else {
						$finalOptions .= '<img src="'.$baseAsset.'/img/layout/icons/action_small_'.$option.'_disabled.png" alt="'.$option.'" border=0"/>';
					}	
				}
				
				echo '<div class="table-datagrid-total">'.PHP_EOL;
					echo '<p>Total: <span>'.$count.'</span></p>'.PHP_EOL;
				echo '</div>'.PHP_EOL;	
								
				if(in_array('add',$actions)) {
					echo '<div class="table-datagrid-actions">'.PHP_EOL;
						echo '<p><span><a href="'.$this->view->url(array('module' => $this->_href['module'], 'controller'=> $this->_href['controller'],'action'=> 'add'), null, true).'">Inserir novo</a></span></p>'.PHP_EOL;
					echo '</div>'.PHP_EOL;	
				}
								
				echo '<table id="'.$this->_nameTable.'" class="table-datagrid" cellspacing="0">'.PHP_EOL;
					echo '<thead>'.PHP_EOL;
					
					echo '<tr>'.PHP_EOL;

						foreach($tableHeaders as $header) {
							if(isset($header['attributes'])) {
							}
							echo '<th>'.$header['label'].'</th>'.PHP_EOL;
						}
						echo '<th>Ações</th>'.PHP_EOL;
							
					echo '</tr>'.PHP_EOL;
					
					echo '</thead>'.PHP_EOL;
	
					echo '<tbody>'.PHP_EOL;
					
					foreach ($paginator as $paginator ) {
						echo '<tr class="table-datagrid-highlight">'.PHP_EOL;
						
						foreach($tableDatas as $data) {
							$data = $paginator[$data];
							if(isset($data)) {
								//If is date...								
								if(count(explode('-',$data)) === 3) {
									$data = $this->view->dateFormat($data);
								}
								echo '<td>'.$data.'</td>'.PHP_EOL;

							} else {
								echo '<td>Nenhum</td>'.PHP_EOL;
							}
						}
						echo '<td>'.$this->_getActions($paginator['id']).'</td>'.PHP_EOL;
						echo '</tr>'.PHP_EOL;
					}
					
					echo '</tbody>'.PHP_EOL;

				echo '</table>'.PHP_EOL;
			}
		}
		
		echo $this->view->paginationControl($this->paginator, 'Sliding', 'index/paginator_control.phtml');
	}
	
}